<template>
  <abstract-dialog
    :loading="loading"
    :title="title"
    :value="value"
    width="35%"
    @close="cancel"
    @open="open"
  >
    <abstract-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="150px"
      label-position="top"
    >
      <el-form-item>
        <div>
          <span>特殊人群名称：{{ info.title }}</span>
          <span style="margin-left: 35px">人数：{{ info.number }}人</span>
          <span style="margin-left: 35px">添加时间：{{ info.addTime }}</span>
        </div>
      </el-form-item>
      <el-form-item label="添加用户(可多选)" prop="users_ids">
        <!-- <el-select placeholder="请选择活动区域" multiple v-model="id"> -->
        <el-select
          ref="elSelect"
          v-model="form.users_ids"
          filterable
          multiple
          collapse-tags
          placeholder="请选择用户"
          @change="selectUser"
        >
          <el-option
            v-for="(i, key) in users"
            :key="key"
            :label="i.name"
            :value="i.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-tag
          v-for="(name, key) in form.users_names"
          :key="key"
          effect="plain"
        >
          {{ name }}
        </el-tag>
      </el-form-item>
    </abstract-form>
    <template v-slot:footer>
      <el-button plain size="small" @click="closeDialog">取 消</el-button>
      <el-button size="small" type="primary" @click="confirm">确 定</el-button>
    </template>
  </abstract-dialog>
</template>

<script>
import AbstractDialog from '@/components/abstract/Dialog'
import dialogMixin from '@/mixin/dialogMixin'
import AbstractForm from '@/components/abstract/Form'
import { elSuccess } from '@/utils/message'
import { request } from '@/api/request'

export default {
  name: 'AddUserDialog',

  components: {
    AbstractDialog,
    AbstractForm
  },

  mixins: [dialogMixin],

  props: {
    id: {
      type: [String, Number],
      default: null
    },
    value: Boolean,
    data: { type: Object, default: () => ({}) },
    position: {
      type: Object,
      default: () => ({
        id: null,
        title: null,
        pathTitle: null
      })
    }
  },

  data() {
    let validateUsersIds = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('至少选择一个用户'))
      } else {
        callback()
      }
    }

    return {
      users_id: '',
      loading: false,
      title: '添加用户',
      form: {
        users_ids: [],
        users_names: []
      },
      info: {},
      users: [],
      rules: {
        users_ids: [
          { required: true, validator: validateUsersIds, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    open() {
      this.loading - true
      request(
        'company/users/specialCrowdSign/addUser',
        { id: this.id },
        (res) => {
          this.users = res.data.users
          this.info = res.data.info
        }
      ).finally(() => (this.loading = false))
    },

    selectUser() {
      this.$nextTick(() => {
        this.form.users_names = this.$refs.elSelect.selected.map(
          (el) => el.label
        )
      })
    },

    clearForm() {
      this.form.users_ids = []
      this.form.users_names = []
      this.$nextTick(() => this.$refs.form.clearValidate())
    },

    cancel() {
      this.closeDialog()
      this.clearForm()
    },

    confirm() {
      if (this.loading) return
      this.$refs.form.validate((v) => {
        if (!v) return
        this.loading = true
        const promise = request(
          'company/users/specialCrowdSign/addUserSubmit',
          { users_ids: JSON.stringify(this.form.users_ids), id: this.id },
          () => {}
        )
        promise
          .then(() => {
            elSuccess()
            this.$emit('success')
            this.cancel()
          })
          .finally(() => this.$nextTick(() => (this.loading = false)))
      })
    }
  }
}
</script>

<style>
.el-tag {
  overflow: hidden;
}
.el-tag::after {
  background-color: aqua;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
</style>
